package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblLoginTypeMySql;
import com.huabo.system.mysql.mapper.TblLoginTypeMySqlMapper;
import com.huabo.system.oracle.entity.TblLoginType;
import com.huabo.system.oracle.mapper.TblLoginTypeMapper;
import com.huabo.system.service.TblLoginTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service("TblLoginTypeService")
@Slf4j
@Transactional
public class TblLoginTypeImpl implements TblLoginTypeService {

    @Resource
    private TblLoginTypeMapper tblLoginTypeMapper;
    @Resource
    private TblLoginTypeMySqlMapper tblLoginTypeMySqlMapper;


    @Override
    public Map<String, Object> findAll(String token, String staffId, Integer pageNumber, Integer pageSize) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                //BigDecimal orgid = new BigDecimal(1);

                PageInfo<TblLoginType> pageInfo = new PageInfo<TblLoginType>();
                if (pageSize != null) {
                    pageInfo.setPageSize(pageSize);
                }
                pageInfo.setCurrentPage(pageNumber);
                try {
                    pageInfo.setTlist(tblLoginTypeMapper.selectListByPageInfo(pageInfo, orgid));
                    pageInfo.setTotalRecord(tblLoginTypeMapper.selectCountByPageInfo(orgid));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            try {
                TblStaffUtil staff = DealUserToken.parseUserToken(token);
                BigDecimal orgid = staff.getCurrentOrg().getOrgid();
                //BigDecimal orgid = new BigDecimal(1);

                PageInfo<TblLoginTypeMySql> pageInfo = new PageInfo<TblLoginTypeMySql>();
                if (pageSize != null) {
                    pageInfo.setPageSize(pageSize);
                }
                pageInfo.setCurrentPage(pageNumber);
                try {
                    pageInfo.setTlist(tblLoginTypeMySqlMapper.selectListByPageInfo(pageInfo, orgid));
                    pageInfo.setTotalRecord(tblLoginTypeMySqlMapper.selectCountByPageInfo(orgid));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                resultMap.put("code", "1");
                resultMap.put("msg", "访问接口成功");
                resultMap.put("data", pageInfo);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return resultMap;
        }
    }

    @Override
    public TblLoginType findByid(String id) {
        return tblLoginTypeMapper.findByLoginId(id);
    }

    @Override
    public TblLoginTypeMySql findByMySqlid(String id) {
        return tblLoginTypeMySqlMapper.findByLoginId(id);
    }

    @Override
    public void updatetblLoginType(TblLoginType tblt) {
        tblLoginTypeMapper.updatetblLoginType(tblt);
    }

    @Override
    public void updateMySqltblLoginType(TblLoginTypeMySql tbltMySql) {
        tblLoginTypeMySqlMapper.updatetblLoginType(tbltMySql);
    }

    @Override
    public void save(TblLoginType tnt) {
        tblLoginTypeMapper.saveTblLoginType(tnt);
    }

    @Override
    public void saveMySql(TblLoginTypeMySql tntMySql) {
        tblLoginTypeMySqlMapper.saveTblLoginType(tntMySql);
    }

    @Override
    public Map<String, Object> findByLoginId(String loginid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            TblLoginType tblLoginType = tblLoginTypeMapper.findByLoginId(loginid);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", tblLoginType);
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            TblLoginTypeMySql tblLoginType = tblLoginTypeMySqlMapper.findByLoginId(loginid);
            resultMap.put("code", "1");
            resultMap.put("msg", "访问接口成功");
            resultMap.put("data", tblLoginType);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> del(String loginid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            tblLoginTypeMapper.deleteByLoginId(loginid);
            resultMap.put("code", "1");
            resultMap.put("msg", "删除成功");
            return resultMap;
        } else {
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            tblLoginTypeMySqlMapper.deleteByLoginId(loginid);
            resultMap.put("code", "1");
            resultMap.put("msg", "删除成功");
            return resultMap;
        }
    }


}
